
cd " "	

use census_pool, clear

keep if ifbirth==1

gen nbirth = 1
replace nbirth = 2 if birth2_male!=.

preserve 
	keep if birth2_male!=.
	rename birth2_male birth_male
	rename birth2_month birth_month
	save tempdata/birth2_info, replace
restore

drop birth2_male birth2_month
rename birth1_male birth_male
rename birth1_month birth_month

append using tempdata/birth2_info

drop if sampleyear==1990

recode birth_month (1 2 3 = 1) (4 5 6 = 2) (7 8 9 = 3) (10 11 12 = 4), gen(birth_quarter) 

gen Q1b = (birth_quarter==1)
gen Q2b = (birth_quarter==2)
gen Q3b = (birth_quarter==3)
gen Q4b = (birth_quarter==4)

gen Q1b_female = Q1b*(1-birth_male)
gen Q2b_female = Q2b*(1-birth_male)
gen Q3b_female = Q3b*(1-birth_male)
gen Q4b_female = Q4b*(1-birth_male)

gen b_female = 1-birth_male

egen prov_sampleyear = group(prov sampleyear)

	
***** Table 2 ******	
	
	sum birth_male 
	sum birth_male if sample==2000
	sum birth_male if sample==2005
	sum birth_male if sample==2010
	sum birth_male if sample==2015

	reghdfe birth_male Q2b Q3b Q4b               , absorb(sampleyear prov) cluster(prov_sampleyear)
		test Q2b Q3b Q4b
		
	reghdfe birth_male Q2b Q3b Q4b   if sample==2000, absorb(sampleyear prov) cluster(prov_sampleyear)
		test Q2b Q3b Q4b
	
	reghdfe birth_male Q2b Q3b Q4b   if sample==2005, absorb(sampleyear prov) cluster(prov_sampleyear)
		test Q2b Q3b Q4b
	
	reghdfe birth_male Q2b Q3b Q4b   if sample==2010, absorb(sampleyear prov) cluster(prov_sampleyear)
		test Q2b Q3b Q4b
	
	reghdfe birth_male Q2b Q3b Q4b   if sample==2015, absorb(sampleyear prov) cluster(prov_sampleyear)
		test Q2b Q3b Q4b
	
***** Table 2  end ******	

	
	gen numbir = numbirf + numbirm
	replace numbir = 1 if numbir == 0
	replace numbir = numbir - nbirth
	replace numbir = 0 if numbir < 0
	
	gen numsur = numsurf + numsurm	
	replace numsur = numsur - nbirth
	replace numsur = 0 if numsur < 0

	gen surbir = numsur / numbir
	
	replace numbirm = numbirm - birth_male
	replace numbirm = 0 if numbirm < 0

	gen brim_yes = (numbirm>0)
	
	egen fem_sampleyear = group(b_female sampleyear)
	egen fem_prov = group(b_female prov)
	
***** Table 3 ******	

	preserve
	drop Q1b Q1b_female
	
	set more off
	reghdfe yedu     Q*b Q*b_female b_female , absorb(sampleyear prov) cluster(prov_sampleyear)  
		test Q2b_female Q3b_female Q4b_female
	reghdfe age      Q*b Q*b_female b_female , absorb(sampleyear prov) cluster(prov_sampleyear)  
		test Q2b_female Q3b_female Q4b_female
	reghdfe urban    Q*b Q*b_female b_female , absorb(sampleyear prov) cluster(prov_sampleyear)  
		test Q2b_female Q3b_female Q4b_female
	reghdfe migrant_hukou   Q*b Q*b_female b_female , absorb(sampleyear prov) cluster(prov_sampleyear)  
		test Q2b_female Q3b_female Q4b_female
	reghdfe numbir   Q*b Q*b_female b_female , absorb(sampleyear prov) cluster(prov_sampleyear)  
		test Q2b_female Q3b_female Q4b_female
	reghdfe brim_yes Q*b Q*b_female b_female , absorb(sampleyear prov) cluster(prov_sampleyear)  
		test Q2b_female Q3b_female Q4b_female
	
	restore
		
	
***** Table 3 End ******	



// Table A2

use census1990_blc, clear

gen male = (sex==1)
gen female = 1 - male
gen prov = int(cntygbnew/10000)
gen newborn = ( age>=98907 )
gen under_5 = ( age>=98507 )
rename age yyymm
gen ybirth = int(yyymm/100)+1000
tab ybirth
gen age = 1990 - ybirth
gen mbirth = yyymm - (ybirth-1000)*100

egen prov_ybirth = group(prov ybirth)
recode mbirth (1 2 3 = 1) (4 5 6 = 2) (7 8 9 = 3) (10 11 12 = 4), gen(birth_quarter) 

gen Q1b = (birth_quarter==1)
gen Q2b = (birth_quarter==2)
gen Q3b = (birth_quarter==3)
gen Q4b = (birth_quarter==4)

gen Q1b_female = Q1b*(1-male)
gen Q2b_female = Q2b*(1-male)
gen Q3b_female = Q3b*(1-male)
gen Q4b_female = Q4b*(1-male)
	
reg male Q2b Q3b Q4b if newborn==1, absorb(prov) cluster(prov)
test Q2b Q3b Q4b 

reghdfe male Q2b Q3b Q4b if under_5==1, absorb(prov ybirth) cluster(prov_ybirth)
test Q2b Q3b Q4b 

